package com.freedom.mediaplayer.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.freedom.mediaplayer.R;
import com.freedom.mediaplayer.view.LazyViewPager;
import com.freedom.mediaplayer.view.LazyViewPager.OnPageChangeListener;

public class HomeFragment extends BaseFragment implements OnClickListener {
	
	//@ViewInject(R.id.viewpager)
	private LazyViewPager pager;
	//@ViewInject(R.id.lrc)
	private TextView lrc;
	//@ViewInject(R.id.imageView)
	private ImageView imageView;
	//@ViewInject(R.id.play_list)
	private TextView playList;
	//@ViewInject(R.id.playing)
	
	private TextView playing;
	private int imageWidth;
	private int index;
	private int offSet;
	private List<BaseFragment> page;

	private int screenWidth;

	public void initData(Bundle paramBundle) {
		page = new ArrayList<BaseFragment>();
		page.add(new SongMenu());
		page.add(new MediaCenter());
		page.add(new LrcPage());
		pager.setAdapter(new PageAdapter(getChildFragmentManager()));
		pager.setCurrentItem(0);
		
		//获取屏幕的宽度
		WindowManager wm  = (WindowManager) ct.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		screenWidth = outMetrics.widthPixels;
		imageWidth = BitmapFactory.decodeResource(getResources(), R.drawable.roller).getWidth();
		offSet = (screenWidth/3-imageWidth)/2;
		
		Matrix matrix = new Matrix();
		matrix.setTranslate(offSet, 0);
		imageView.setImageMatrix(matrix);
		pager.setOnPageChangeListener(new OnPageChangeListener() {
			int preDistance = offSet * 2 + imageWidth;
			@Override
			public void onPageSelected(int position) {
				TranslateAnimation animation = new TranslateAnimation(preDistance * index, position * preDistance, 0, 0);
				animation.setDuration(510);
				animation.setFillAfter(true);
				imageView.startAnimation(animation);
				index = position;
			}

			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int state) {
				// TODO Auto-generated method stub

			}
		});
		playList.setOnClickListener(this);
		playing.setOnClickListener(this);
		lrc.setOnClickListener(this);
	}

	@Override
	public View initView(LayoutInflater inflater) {
		// TODO Auto-generated method stub
		view = inflater.inflate(R.layout.frag_home, null);
		pager = (LazyViewPager) view.findViewById(R.id.viewpager);
		lrc = (TextView) view.findViewById(R.id.lrc);
		imageView = (ImageView) view.findViewById(R.id.imageView);
		playList = (TextView) view.findViewById(R.id.play_list);
		playing = (TextView) view.findViewById(R.id.playing);
		//ViewUtils.inject(this, view);
		return view;
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.play_list:
			pager.setCurrentItem(0, false);
			return;
		case R.id.playing:
			pager.setCurrentItem(1, false);
			return;
		case R.id.lrc:
			pager.setCurrentItem(2, false);
			return;
		}
	}
	class PageAdapter extends FragmentPagerAdapter {

		public PageAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return page.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return page.size();
		}
	}
}
